package cn.cstech.aijia.ui.base.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import cn.cstech.aijia.R;
import cn.cstech.aijia.sns.SnsShareActivity;

public class DialogUtil {

	/**
	 * 获取加载等待对话框
	 * @param context
	 * @param message
	 * @param isCancelable
	 * @return
	 */
	public static LoadingDialog showLoadingDialog(Context context,String message , boolean isCancelable){
		LoadingDialog dialog = new LoadingDialog(context,R.style.loading_dialog);
		dialog.setCancelable(isCancelable);
		dialog.setDlgMessage(message);
//		dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
		return dialog;
	}

	/**
	 * 弹出自定义的对话框
	 * @param context
	 * @param title
	 * @param content
	 * @param positiveBtnLabel
	 * @param negativeBtnLabel
	 * @param posLsn
	 * @param ngLsn
	 * @return
	 */
	public static Dialog showMessageDialog(Context context,
										   String title,
										   String content,
										   String positiveBtnLabel,
										   String negativeBtnLabel,
										   View.OnClickListener posLsn,
										   View.OnClickListener ngLsn){
		LayoutInflater mLayoutInflater = LayoutInflater.from(context);
		View view = mLayoutInflater.inflate(R.layout.custom_msg_dlg, null);
		final Dialog dialog = new Dialog(context, R.style.kangzai_dialog);
//		dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		dialog.setContentView(view);
		dialog.setCancelable(false);

		TextView msg_dlg_title;
		TextView msg_dlg_content;
		Button msg_dialog_btn_cancel;
		Button msg_dialog_btn_ok;

		msg_dlg_title = (TextView)view.findViewById(R.id.msg_dlg_title);
		msg_dlg_content = (TextView)view.findViewById(R.id.msg_dlg_content);
		msg_dialog_btn_cancel = (Button)view.findViewById(R.id.msg_dialog_btn_cancel);
		msg_dialog_btn_ok = (Button)view.findViewById(R.id.msg_dialog_btn_ok);
		if(title != null){
			msg_dlg_title.setText(title);
		}else{
			msg_dlg_title.setVisibility(View.GONE);
		}
		if(content != null){
			msg_dlg_content.setText(content);
		}else{
			msg_dlg_content.setVisibility(View.GONE);
		}
		if(negativeBtnLabel != null){
			msg_dialog_btn_cancel.setText(negativeBtnLabel);
		}else{
			msg_dialog_btn_cancel.setVisibility(View.GONE);
		}
		if( positiveBtnLabel!= null){
			msg_dialog_btn_ok.setText(positiveBtnLabel);
		}else{
			msg_dialog_btn_ok.setVisibility(View.GONE);
		}
		if(ngLsn == null){
			msg_dialog_btn_cancel.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
		}else{
			msg_dialog_btn_cancel.setOnClickListener(ngLsn);
		}
		if (null == posLsn) {
			msg_dialog_btn_ok.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (null != dialog) {
						dialog.dismiss();
					}
				}
			});
		} else {
			msg_dialog_btn_ok.setOnClickListener(posLsn);
		}
		return dialog;
	}

	/**
	 * 提示消息对话框
	 * @param context
	 * @param title
	 * @param content
	 * @param positiveBtnLabel
	 * @param negativeBtnLabel
	 * @param posLsn
	 * @param ngLsn
	 * @param leftBtnStyle
	 * @param rightBtnStyle
	 * @return
	 */
	public static Dialog showMessageDialog(Context context,
										   String title,
										   String content,
										   String positiveBtnLabel,
										   String negativeBtnLabel,
										   View.OnClickListener posLsn,
										   View.OnClickListener ngLsn,int leftBtnStyle, int
												   rightBtnStyle){
		Dialog dialog = showMessageDialog(context,title,content,positiveBtnLabel,
				negativeBtnLabel, posLsn,ngLsn);
		if (null != dialog) {
			Button btnLeft = (Button) dialog.findViewById(R.id.msg_dialog_btn_cancel);
			Button btnRight = (Button) dialog.findViewById(R.id.msg_dialog_btn_ok);
			btnLeft.setTextAppearance(context,leftBtnStyle);
			btnRight.setTextAppearance(context,rightBtnStyle);
		}
		return dialog;
	}

	/**
	 * 分享接口
	 * @param context
	 * @param shareTitle 分享的title
	 * @param shareContent 分享的内容
	 * @param shareImageUrl 分享图片链接
	 * @param shareImageLocal 本地图片
	 */
	public static void showShareDialog(Context context,
									   String shareTitle,
									   String shareContent,
									   String shareWebPage,
									   String shareImageUrl,
									   String shareImageLocal,
									   String shareAction,
									   Bitmap thumb,
									   String appendAction
	){
		Intent intent = new Intent(context,SnsShareActivity.class);
		if(!TextUtils.isEmpty(shareTitle)){
			intent.putExtra(SPUtils.EXTRA_SHARE_TITLE, shareTitle);
		}
		if(!TextUtils.isEmpty(shareContent)){
			intent.putExtra(SPUtils.EXTRA_SHARE_CONTENT, shareContent);
		}
		if(!TextUtils.isEmpty(shareImageUrl)){
			intent.putExtra(SPUtils.EXTRA_SHARE_IMAGE_URL, shareImageUrl);
		}
		if(!TextUtils.isEmpty(shareImageLocal)){
			intent.putExtra(SPUtils.EXTRA_SHARE_IMAGE_LOCAL, shareImageLocal);
		}
		if(!TextUtils.isEmpty(shareWebPage)){
			intent.putExtra(SPUtils.EXTRA_SHARE_WEB_PAGE, shareWebPage);
		}
		if(!TextUtils.isEmpty(shareAction)){
			intent.putExtra(SPUtils.EXTRA_SHARE_ACTION, shareAction);
		}

		if(thumb != null){
			intent.putExtra(SPUtils.EXTRA_SHARE_WEB_PAGE_THUMB, thumb);
		}
		
		context.startActivity(intent);
		if(context instanceof Activity){
			((Activity)context).overridePendingTransition(R.anim.push_bottom_in,R.anim.push_bottom_out);
		}
	}
}
